package com.shanzmoo.core.config;

import cn.hutool.core.util.StrUtil;
import com.alipay.easysdk.base.oauth.Client;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.shanzmoo.core.domain.AllAlipayClient;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * 支付宝配置属性类
 *
 * @author fs
 */
@Data
@Component
@ConfigurationProperties(prefix = "alipay.prop")
@RequiredArgsConstructor
public class AlipayProperties {
    /**
     * 支付宝应用id
     */
    private String alipayAppId;
    /**
     * 应用私钥
     */
    private String alipayPrivateKey;
    /**
     * 支付宝公钥
     */
    private String alipayPublicKey;
    /**
     * 加密串
     */
    private String encryptKey;

    @Bean
    public AllAlipayClient allAlipayClient() throws Exception{
        AllAlipayClient allAlipayClient = new AllAlipayClient();
        allAlipayClient.setUser(createAlipayClient(null));
        return allAlipayClient;
    }
    private Client createAlipayClient(String notifyUrl) throws Exception{
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = "openapi.alipay.com";
        config.signType = "RSA2";
        config.appId = this.alipayAppId;
        config.merchantPrivateKey = this.alipayPrivateKey;
        config.alipayPublicKey = alipayPublicKey;
        if(StrUtil.isNotBlank(this.encryptKey)){
            config.encryptKey=this.encryptKey;
        }
        if(StrUtil.isNotBlank(notifyUrl)){
            config.notifyUrl = notifyUrl;
        }
        Factory.setOptions(config);
        return Factory.Base.OAuth();
    }
}
